package com.qxy.practice.concurrent;

import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLockExample {
    private final ReentrantLock lock = new ReentrantLock();

    public void performTask() {
        lock.lock();
        try {
            // 临界区代码，需要同步的部分
            System.out.println("Thread " + Thread.currentThread().getName() + " is performing the task.");
        } finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        ReentrantLockExample example = new ReentrantLockExample();
        Thread t1 = new Thread(example::performTask);
        Thread t2 = new Thread(example::performTask);
        t1.start();
        t2.start();
    }
}
